package com.bootdo.clouddo.service.article.impl;

import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.bootdo.clouddo.dao.article.ArticleDao;
import com.bootdo.clouddo.domain.article.ArticleDO;
import com.bootdo.clouddo.service.article.IArticleService;
import com.bootdo.clouddo.utils.StringUtils;

@Service
public class ArticleServiceImpl implements IArticleService {
	
	@Autowired
	private ArticleDao dao;

	@Override
	public int save(ArticleDO post) {
		return dao.insertSelective(post);
	}

	@Override
	public ArticleDO get(String id) {
		return dao.selectByPrimaryKey(id);
	}

	@Override
	public List<ArticleDO> find(String category[], String wd, String channel, Integer page, Integer size) {
		StringBuilder whereStr = new StringBuilder(" 1=1 ");
		if(StringUtils.isNotBlank(wd))
			whereStr.append("and title like '%" + wd + "%'");
		if(category != null && category.length >0)
			whereStr.append("and type in ('" + StringUtils.join(category, "','") + "')");

		return dao.find(whereStr.toString(), "create_time", "desc", new RowBounds(page, size));
	}

	@Override
	public List<ArticleDO> findByUser(String[] category, String wd, String channel, Integer page, Integer size) {
		StringBuilder whereStr = new StringBuilder();
		whereStr.append("author_id = '" + wd + "'");
		if(category != null && category.length >0)
			whereStr.append("and type in ('" + StringUtils.join(category, "','") + "')");

		return dao.find(whereStr.toString(), "create_time", "desc", new RowBounds(page, size));
	}

	@Override
	public Map<String, ?> countByUser(String uid) {
		return dao.countByUser(uid);
	}
	
}
